import { message } from 'antd';
import {
  suAccount,
  getMgrSuppList,
  insertAccount,
  updateAccount,
  updateAccountStatus,
  deleteAccount,
  queryAccountById,
} from '@/services/supplymanage';

export default {
  state: {
    pageData: {
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
    formFieldsValue: {},
    supplyNameList: [],
    userType: 1,
    addPort: [],
    corpId: '',
    suppCodeLs: '',
    name: '',
    suppUserId: '',
    userId: '',
    detailFormValue: {},
    supplyName: '',
    valuesArr: [
      'suppCode',
      'name',
    ]
  },
  effects: {
    *queryAccountById({ payload }, { call, put, select }) {
      const res = yield call(queryAccountById, payload);
      const supplyNameList = yield select(state => state.supplieraccountmanage.supplyNameList);
      let supplyName = '';
      if (res.success) {
        if (supplyNameList.some(item => item.suppId === res.data.suppId)) {
          supplyName = '';
        } else {
          supplyName = res.data.suppName;
        }
        yield put({
          type: 'updateModelData',
          payload: {
            supplyName,
            detailFormValue: res.data,
            suppUserId: res.data.suppUserId,
            userId: res.data.userId,
            corpId: res.data.corpId,
            suppCodeLs: res.data.suppCode,
            name: res.data.suppName,
          },
        });
      } else {
        message.warning(res.message || '服务端错误');
      }
      return res;
    },
    *deleteAccount({ payload }, { call }) {
      const res = yield call(deleteAccount, payload);
      if (res.success) {
        message.success('删除成功');
      } else {
        message.warning(res.message || '服务端错误');
      }
      return res;
    },
    *updateAccountStatus({ payload }, { call }) {
      const res = yield call(updateAccountStatus, payload);
      return res;
    },
    *updateAccount({ payload }, { call }) {
      const res = yield call(updateAccount, payload);
      return res;
    },
    *insertAccount({ payload }, { call }) {
      const res = yield call(insertAccount, payload);
      return res;
    },
    *getMgrSuppList({ payload }, { call, put }) {
      const res = yield call(getMgrSuppList, payload);
      if (res.success) {
        yield put({
          type: 'updateModelData',
          payload: { supplyNameList: res.data },
        });
      } else {
        message.warning(res.message || '服务端错误');
      }
    },
    // 获取业务范围查看列表
    *getPageList({ payload }, { call, put }) {
      const response = yield call(suAccount, payload);
      const {
        pageArg: { currentPage, pageSize },
      } = payload;
      if (response.success) {
        yield put({
          type: 'getPageLookList',
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        message.warning(response.message || '服务端错误');
      }
    },
  },
  reducers: {
    // 获取业务范围查看列表
    getPageLookList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = Array.isArray(data.pageRecords) ? data.pageRecords : [];
      // eslint-disable-next-line array-callback-return
      return {
        ...state,
        pageData: {
          list,
          pagination: { total: data.totalCount, current: currentPage, pageSize },
        },
      };
    },
    // 设置修改当前state数据
    updateModelData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    // 设置页签状态
    updateTabListQuery(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
